package com.ruoyi.common.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class PdfToExcelConverter {

    // 定义标题列表
    private static final List<String> TITLES = Arrays.asList(
            "序号", "标题", "所属领域", "痛点问题", "解决方案",
            "竞争优势分析", "市场应用场景", "发展规划", "合作需求",
            "团队介绍", "联系方式", "成果编号"
    );

    public static void main(String[] args) {
        String pdfPath = "E:\\workdata\\码诚科技\\成果解析\\待录入文件\\清华大学2023.pdf"; // PDF文件路径
        String excelPath = "E:\\workdata\\码诚科技\\成果解析\\待录入文件\\清华大学2023.xlsx"; // 输出的Excel文件路径

        try {
            // 读取PDF文件并提取文本
            String text = extractTextFromPDF(pdfPath);

            // 提取各个标题及对应的内容
            Map<String, List<String>> contentMap = extractContentByTitles(text);

            // 创建Excel文件并写入数据
            writeToExcel(contentMap, excelPath);

            System.out.println("PDF内容已成功转换为Excel: " + excelPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 提取PDF文本
    private static String extractTextFromPDF(String pdfPath) throws IOException {
        try (PDDocument document = PDDocument.load(new File(pdfPath))) {
            PDFTextStripper stripper = new PDFTextStripper();
            return stripper.getText(document);
        }
    }

    // 提取每个标题下的内容
    private static Map<String, List<String>> extractContentByTitles(String text) {
        Map<String, List<String>> contentMap = new HashMap<>();

        for (String title : TITLES) {
            contentMap.put(title, new ArrayList<>());
        }

        // 使用正则表达式提取标题和内容
        for (String title : TITLES) {
            Pattern pattern = Pattern.compile(title + "(.*?)(?=\n\\S+:|$)", Pattern.DOTALL);
            Matcher matcher = pattern.matcher(text);

            while (matcher.find()) {
                String content = matcher.group(1).trim();
                contentMap.get(title).add(content);
            }
        }

        return contentMap;
    }

    // 将数据写入Excel
    private static void writeToExcel(Map<String, List<String>> contentMap, String excelPath) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("PDF 内容");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < TITLES.size(); i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(TITLES.get(i));
        }

        // 获取最大列的长度
        int maxRows = contentMap.values().stream().mapToInt(List::size).max().orElse(0);

        // 填充表格内容
        for (int rowIndex = 0; rowIndex < maxRows; rowIndex++) {
            Row row = sheet.createRow(rowIndex + 1); // 跳过表头行
            for (int colIndex = 0; colIndex < TITLES.size(); colIndex++) {
                String title = TITLES.get(colIndex);
                List<String> contentList = contentMap.get(title);

                // 确保每列的内容对齐
                if (rowIndex < contentList.size()) {
                    Cell cell = row.createCell(colIndex);
                    cell.setCellValue(contentList.get(rowIndex));
                }
            }
        }

        // 将Excel保存到文件
        try (FileOutputStream fileOut = new FileOutputStream(excelPath)) {
            workbook.write(fileOut);
        } finally {
            workbook.close();
        }
    }
}

